package com.ebt.m.presenter;

import android.app.Activity;

import com.ebt.m.AppInitService;
import com.ebt.m.commons.buscomponent.listview.BasePresenter;
import com.ebt.m.commons.model.data.BaseDataResult;
import com.ebt.m.commons.utils.RxUtil;
import com.ebt.m.data.bean.CardInfo;
import com.ebt.m.data.bean.UserInfo;
import com.ebt.m.data.rxModel.api.EBTAPI;
import com.ebt.m.data.rxModel.apibean.DynamicsCountData;
import com.ebt.m.commons.utils.Logger;
import com.ebt.m.commons.utils.NetUtils;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class HomePagePresenter extends BasePresenter<HomePageContract.IView> implements HomePageContract.IPresenter {


    public HomePagePresenter(Activity context, HomePageContract.IView iView) {
        super(context, iView);
    }

    @Override
    public void getActiveCustomerData() {
        EBTAPI userApi = AppInitService.getEbtApi();
        userApi.getDynamicsCount()
                .compose(RxUtil.rxTransformer(iView))
                .subscribe(resultBean -> {
                    try {
                        DynamicsCountData dynamicsCountData = resultBean.getData();
                        iView.showActiveCustomer(dynamicsCountData);
                    } catch (Exception e) {
                        Logger.e(e);
                        iView.showActiveCustomer(null);
                    }
                }, throwable -> {
                    throwable.printStackTrace();
                    iView.showActiveCustomer(null);
                });
    }


    @Override
    public void getPerformanceData() {
        EBTAPI erpApi = AppInitService.getEbtApi();
        erpApi.getPersonalPerformance()
                .compose(RxUtil.rxTransformer(iView))
                .subscribe(performanceAll -> {
                    iView.updatePerformanceView(performanceAll.getData());
                }, throwable -> {
                    Logger.e(throwable.getMessage());
                    iView.updatePerformanceView(null);
                });
    }

    @Override
    public void getAuthenticationStatus() {
        EBTAPI erpApi = AppInitService.getEbtApi();
        erpApi.isAuthenticate()
                .flatMap(resultBaseDataResult -> {
                    UserInfo userInfo = resultBaseDataResult.getData();
                    return erpApi.getERPCardInfo()
                            .onErrorReturn(throwable -> new BaseDataResult<>())
                            .map(cardInfoBaseDataResult -> {
                                if (cardInfoBaseDataResult != null && cardInfoBaseDataResult.getData() != null) {
                                    CardInfo cardInfo = cardInfoBaseDataResult.getData();
                                    userInfo.setCardInfo(cardInfo);
                                }
                                return userInfo;
                            });
                })
                .compose(RxUtil.rxTransformer(iView))
                .subscribe(userInfo ->
                                iView.updateRegisterCompandy(userInfo)
                        , throwable -> {
                            Logger.e(throwable.getMessage());
                            NetUtils.checkHttpException(mContext, throwable);
                            iView.updateRegisterCompandy(null);
                        });
    }
}
